package UPPM;

import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

import UPPM.Utils;

public class TestThemedPlaylists {
	WebDriver driver = new FirefoxDriver();

	@Before
	public void setUp() throws Exception {
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		driver.manage().deleteAllCookies();
		driver.get("http://test.unippmtest.co.uk");		
			
		}
	
	@Test
    public void testThemedPlUiInitialStateUserNotLogedIn() throws Exception {
		
		Utils.waitForElement(driver, By.linkText("Playlists"));
		Thread.sleep(3000);
		driver.findElement(By.linkText("Playlists")).click();
		
			
		Utils.waitForElement(driver, By.xpath(".//*[@id='pageRows']/div[1]/div[1]/div/div/div/div[1]/div[1]/div/a"));
		WebElement thPlaylist_left = driver.findElement(By.xpath(".//*[@id='playlist_details']/table/tbody/tr[1]/td/div/img"));
		Assert.assertEquals(true, thPlaylist_left.isDisplayed());	
		
		
		WebElement click_pl = driver.findElement(By.partialLinkText("Best Of"));
		click_pl.click();
		Utils.waitForElement(driver, By.className("playlist_title"));
		//check details
		WebElement Playlist_title = driver.findElement(By.className("playlist_title"));
		Assert.assertEquals(true, Playlist_title.isDisplayed());
		WebElement Playlist_header = driver.findElement(By.className("playlist_hdr"));
		Assert.assertEquals(true, Playlist_header.isDisplayed());
		
	}
	
    @Test
    public void testThemedPlUiInitialStateUserLogedIn() throws Exception {
    	
	    Utils.Login(driver);
	    
	    Utils.waitForElement(driver, By.linkText("Playlists"));
		driver.findElement(By.xpath(".//*[@id='sub-nav-menu']/li[2]/a")).click();
		
	
		Utils.waitForElement(driver, By.xpath(".//*[@id='pageRows']/div[1]/div[1]/div/div/div/div[1]/div[1]/div/a"));
		WebElement thPlaylist_left = driver.findElement(By.xpath(".//*[@id='playlist_details']/table/tbody/tr[1]/td/div/img"));
		Assert.assertEquals(true, thPlaylist_left.isDisplayed());	
		
		
		WebElement click_pl = driver.findElement(By.partialLinkText("Best Of"));
		click_pl.click();
		Utils.waitForElement(driver, By.className("playlist_title"));
		//check details
		WebElement Playlist_title = driver.findElement(By.className("playlist_title"));
		Assert.assertEquals(true, Playlist_title.isDisplayed());
		WebElement Playlist_header = driver.findElement(By.className("playlist_hdr"));
		Assert.assertEquals(true, Playlist_header.isDisplayed());
	
		
		WebElement cd_number = driver.findElement(By.xpath(".//*[@id='playlist_details']/div[2]/div/div[4]/div[1]"));
		Assert.assertEquals("CD #", cd_number.getText());
		WebElement track_number = driver.findElement(By.xpath(".//*[@id='playlist_details']/div[2]/div/div[4]/div[2]"));
		Assert.assertEquals( "#", track_number.getText());
		WebElement track_title = driver.findElement(By.xpath(".//*[@id='playlist_details']/div[2]/div/div[4]/div[3]"));
		Assert.assertEquals("Title", track_title.getText());
		WebElement track_desc = driver.findElement(By.xpath(".//*[@id='playlist_details']/div[2]/div/div[4]/div[4]"));
		Assert.assertEquals("Description", track_desc.getText());
		WebElement version = driver.findElement(By.xpath(".//*[@id='playlist_details']/div[2]/div/div[4]/div[5]"));
		Assert.assertEquals("Version", version.getText());
		WebElement comp = driver.findElement(By.xpath(".//*[@id='playlist_details']/div[2]/div/div[4]/div[6]"));
		Assert.assertEquals("Composers", comp.getText());
		WebElement publisher = driver.findElement(By.xpath(".//*[@id='playlist_details']/div[2]/div/div[4]/div[7]"));
		Assert.assertEquals("Publishers", publisher.getText());
		WebElement actions = driver.findElement(By.xpath(".//*[@id='playlist_details']/div[2]/div/div[4]/div[8]"));
		Assert.assertEquals("Actions", actions.getText());
		
		
     }
    

    
   	//check add themed playlist to project; try to add it again; try to download it; delete themed paylist from project;
	@Test
    public void testAddThemedPlaylistToProject() throws Exception {
		
		
		Utils.Login(driver);
	    
	    Utils.waitForElement(driver, By.linkText("Playlists"));
//	    WebElement playlists = driver.findElement(By.xpath(".//*[@id='sub-nav-menu']/li[2]/a"));
	    driver.findElement(By.xpath(".//*[@id='sub-nav-menu']/li[2]/a")).click();

//	    Actions builder = new Actions(driver);
//		builder.click(playlists).perform();
		
		
	
		Utils.waitForElement(driver, By.xpath(".//*[@id='pageRows']/div[1]/div[1]/div/div/div/div[1]/div[1]/div/a"));
		WebElement thPlaylist_left = driver.findElement(By.xpath(".//*[@id='playlist_details']/table/tbody/tr[1]/td/div/img"));
		Assert.assertEquals(true, thPlaylist_left.isDisplayed());	
		
		
		WebElement click_pl = driver.findElement(By.partialLinkText("Best Of"));
		click_pl.click();
		
		Utils.waitForElement(driver, By.className("playlist_title"));
		//check details
		WebElement Playlist_title = driver.findElement(By.className("playlist_title"));
		Assert.assertEquals(true, Playlist_title.isDisplayed());
		WebElement Playlist_header = driver.findElement(By.className("playlist_hdr"));
		Assert.assertEquals(true, Playlist_header.isDisplayed());
	
	   	
	   	WebElement add_to_proj = driver.findElement(By.linkText("Add to current project"));
	   	add_to_proj.click();
	   
	   	Utils.waitForElement(driver, By.id("error_overlay"));
	   	
	   	WebElement close_warning = driver.findElement(By.xpath("//*[@id='error_overlay']/a"));
	   	close_warning.click();
	   	
	   	//expand first project in the list
	   	
	   	WebElement expand_proj = driver.findElement(By.xpath("//*[@id='exp1']/div[1]/div[1]/div/div[2]/a/span/span[1]"));
	   	expand_proj.click();
	   	Thread.sleep(3000L);
	   	add_to_proj.click();
	   	WebElement added_proj = driver.findElement(By.xpath("//*[@id='exp1']/div[1]/div[2]/div[2]/div[1]/div/div[2]/a/span/span[2]"));
	   	WebElement pl_title = driver.findElement(By.xpath(".//*[@id='exp1']/div[1]/div[2]/div[2]/div[1]/div/div[2]/a/span/span[2]"));
	   	Assert.assertEquals(added_proj.getText().trim(), pl_title.getText().trim());
	   	
	   	//try to add again to check warning message
	   	add_to_proj.click();
	   	Utils.waitForElement(driver, By.id("error_overlay"));
	   	close_warning.click();
	   	
	   	//try to download playlist from miniproj
	   	WebElement dld_pl = driver.findElement(By.xpath("//*[@id='exp1']/div[1]/div[2]/div[2]/div[1]/div/div[1]/a[2]"));
	   	dld_pl.click();
	   	Utils.waitForElement(driver, By.id("audioFormats_overlay"));
	   	
	   	WebElement closebtn = driver.findElement(By.xpath(".//*[@id='audioFormats_overlay']/a"));
	   	closebtn.click();
	   	
	
   }
    
    @After
    public void tearDown() throws Exception {
//	driver.quit();
}


}
